package com.sfjswl.fp.core.base;

import com.sfjswl.fp.common.enums.NodeRunStateEnum;
import com.sfjswl.fp.common.enums.TimeTypeEnum;
import com.sfjswl.fp.core.context.RuleContext;
import com.sfjswl.fp.core.utils.ProcessUtils;
import com.sfjswl.fp.core.utils.RuleTimeUtils;
import lombok.Data;

@Data
public abstract class BaseNode {

  private long ruleNodeId;

  private TimeTypeEnum ruleTimeTypeEnum;
  /**
   * 节点开始运行时间
   */
  private long ruleStart;
  /**
   * 节点结束运行时间
   */
  private long ruleEnd;

  private String ruleLogName;

  protected abstract NodeRunStateEnum processNode(RuleContext ctx);

  public NodeRunStateEnum process(RuleContext ctx) {
    if (RuleTimeUtils.timeDisable(ruleTimeTypeEnum, ctx.getPack().getRequestTime(), ruleStart, ruleEnd)) {
      ProcessUtils.collectInfo(ctx.getProcessInfo(), this, 'O');
      return NodeRunStateEnum.NONE;
    }
    long start = System.currentTimeMillis();
    NodeRunStateEnum res;
    try {
      res = processNode(ctx);
    } catch (Throwable t) {
      ProcessUtils.collectInfo(ctx.getProcessInfo(), this, start, NodeRunStateEnum.SHUT_DOWN);
      throw t;
    }
    ProcessUtils.collectInfo(ctx.getProcessInfo(), this, start, res);
    return res;
  }
}
